package itbour.onetouchshow.widget;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatTextView;
import android.util.AttributeSet;
import android.view.View;

import itbour.onetouchshow.R;
import itbour.onetouchshow.utils.DisplayUtil;

/**
 * Created by guoh on 2018/8/2.
 * 功能描述：带字体的EditTextView
 * 需要的参数：
 */
public class FontTextView extends AppCompatTextView {


    private onSizeChangedListener onSizeChangedListener;

    public FontTextView(Context context) {
        super(context);
        setDefaultFontStyle();
        setBackGroundStyle();
        setDefaultText();
    }

    public FontTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        setDefaultFontStyle();
        setBackGroundStyle();
        setDefaultText();
    }

    public FontTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setDefaultFontStyle();
        setBackGroundStyle();
        setDefaultText();
    }

    public void setDefaultFontStyle() {
        this.setTypeface(null);
    }

    public void setBlackFontStyle() {
        Typeface typeface = Typeface.createFromAsset(getContext().getAssets(), "Fonts/FZHei_B01S.ttf");
        this.setTypeface(typeface);
    }

    private void setBackGroundStyle() {
        this.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        this.setBackground(getResources().getDrawable(R.drawable.letter_style));
        this.setPadding(DisplayUtil.dip2px(getContext(), 10), DisplayUtil.dip2px(getContext(), 5), DisplayUtil.dip2px(getContext(), 10), DisplayUtil.dip2px(getContext(), 5));
    }

    private void setDefaultText() {
        this.setText("双击可编辑");
        this.setTextColor(Color.WHITE);
        this.setTextSize(14);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        if (onSizeChangedListener != null) {
            onSizeChangedListener.onSizeChanged(w, h);
        }
    }

    public void setOnSizeChangedListener(FontTextView.onSizeChangedListener onSizeChangedListener) {
        this.onSizeChangedListener = onSizeChangedListener;
    }

    public interface onSizeChangedListener {
        void onSizeChanged(int w, int h);
    }

}
